<?php
include dirname(dirname(dirname(__FILE__))).'/include/db_connect.php';
include_once dirname(dirname(dirname(__FILE__))).'/include/host-base.php';

function getSysConfigId() {
	$sql = 'select id from sys_config limit 0,1';
	$conn = get_db_conn();
	$result = mysql_query($sql,$conn) or die("db error");
	if(mysql_num_rows($result)==0) {
		$sql = 'insert into sys_config(is_timer_on,is_front_on,is_timer_now_on,server_start_time) values(0,0,0,"'.time().'");';
		mysql_query($sql,$conn) or die("db error");
		$sql = 'select id from sys_config limit 0,1';
		$result = mysql_query($sql,$conn) or die("db error");
	}
	$row = mysql_fetch_array($result);
	return $row['id'];
}

function isTimerOn() {
	$sql = 'select is_timer_on from sys_config limit 0,1';
	$conn = get_db_conn();
	$result = mysql_query($sql,$conn) or die("db error");
	$row = mysql_fetch_array($result);
	return $row['is_timer_on'];
}

function isFrontOn() {
	$sql = 'select is_front_on from sys_config limit 0,1';
	$conn = get_db_conn();
	$result = mysql_query($sql,$conn) or die("db error");
	$row = mysql_fetch_array($result);
	return $row['is_front_on'];
}

function setIsTimerOn($is_on) {
	$is_on = addslashes($is_on);
	$conn = get_db_conn();
	$id = getSysConfigId();
	$sql = 'update sys_config set is_timer_on = '.$is_on.' where id = '.$id.';';
	mysql_query($sql,$conn) or die(mysql_error());
	return true;
}

function setIsFrontOn($is_on) {
	$is_on = addslashes($is_on);
	$conn = get_db_conn();
	$id = getSysConfigId();
	$sql = 'update sys_config set is_front_on = '.$is_on.' where id = '.$id.';';
	mysql_query($sql,$conn) or die("db error");
	return true;
}

function isTimerNowOn() {
	$sql = 'select is_timer_now_on from sys_config limit 0,1';
	$conn = get_db_conn();
	$result = mysql_query($sql,$conn) or die("db error");
	$row = mysql_fetch_array($result);
	return $row['is_timer_now_on'];
}

function setIsTimerNowOn($is_on) {
	$is_on = addslashes($is_on);
	$conn = get_db_conn();
	$id = getSysConfigId();
	$sql = 'update sys_config set is_timer_now_on = '.$is_on.' where id = '.$id.';';
	mysql_query($sql,$conn) or die("db error");
	return true;
}

function getServerRunTime() {
	$conn = get_db_conn();
	$id = getSysConfigId();
	$sql = 'select server_start_time from sys_config where id = '.$id.' limit 0,1;';
	$result = mysql_query($sql,$conn);
	$row = mysql_fetch_array($result);
	return $row['server_start_time'];
}